package com.lanyou.esb.service;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Tuple;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.lanyou.esb.entity.DefinedBundle;

/**
 * 非内核BUNDLE服务类
 * 
 * @author Davey.wu
 */
@Service
public class DefinedBundleService {

	@PersistenceContext
	private EntityManager entityManager;

	/**
	 * 根据编码集合获取BUNDLE
	 * 
	 * @param codeList
	 * @return
	 */
	public List<DefinedBundle> getDefinedBundleByCode(List<String> codeList) {
		CriteriaBuilder cb = entityManager.getCriteriaBuilder();
		CriteriaQuery<Tuple> cq = cb.createTupleQuery();

		// 构建FROM和JOIN部分
		Root<DefinedBundle> rootBundle = cq.from(DefinedBundle.class);

		// 构建SELECT部分
		cq.multiselect(rootBundle.get("bundleCode").alias("bundleCode"),
				rootBundle.get("type").alias("type"));

		// 构建WHERE部分
		List<Predicate> predicatesList = new ArrayList<>();
		if (codeList != null && codeList.size() > 0) {
			predicatesList.add(rootBundle.get("bundleCode").in(codeList));
		}
		if (predicatesList.size() > 0) {
			cq.where(predicatesList.toArray(new Predicate[predicatesList.size()]));
		}

		// 执行查询
		TypedQuery<Tuple> q = entityManager.createQuery(cq);

		List<Tuple> queryResult = q.getResultList();

		if (queryResult == null || queryResult.size() < 1) {
			return new ArrayList<>(0);
		}
		List<DefinedBundle> result = new ArrayList<>(queryResult.size());

		for (Tuple tuple : queryResult) {
			DefinedBundle bundle = new DefinedBundle();
			bundle.setBundleCode((String) tuple.get("bundleCode"));
			bundle.setType((String) tuple.get("type"));
			result.add(bundle);
		}
		return result;
	}

	/**
	 * 获取BUNDLE
	 * 
	 * @param name
	 *            :名称
	 * @param jarName
	 *            ：JAR包名
	 * @param types
	 *            ：类型集合
	 * @return
	 */
	public List<DefinedBundle> getDefinedBundle(String name, String jarName,
			List<String> types) {
		CriteriaBuilder cb = entityManager.getCriteriaBuilder();
		CriteriaQuery<Tuple> cq = cb.createTupleQuery();

		// 构建FROM和JOIN部分
		Root<DefinedBundle> rootBundle = cq.from(DefinedBundle.class);

		// 构建SELECT部分
		cq.multiselect(rootBundle.get("bundleCode").alias("bundleCode"),
				rootBundle.get("type").alias("type"));

		// 构建WHERE部分
		List<Predicate> predicatesList = new ArrayList<>();
		if (StringUtils.isNotBlank(name)) {
			predicatesList.add(cb.equal(rootBundle.get("bundleName"), name));
		}
		if (StringUtils.isNotBlank(jarName)) {
			predicatesList.add(cb.like(rootBundle.get("bundleJarName"), jarName
					+ "%"));
		}

		if (types != null && types.size() > 0) {
			predicatesList.add(rootBundle.get("type").in(types));
		}
		// BundleLocation不能为空
		predicatesList.add(cb.isNotNull(rootBundle.get("bundleLocation")));
		// Bundle编码不能为空
		predicatesList.add(cb.isNotNull(rootBundle.get("bundleCode")));
		// Bundle名称不能为空
		predicatesList.add(cb.isNotNull(rootBundle.get("bundleName")));

		if (predicatesList.size() > 0) {
			cq.where(predicatesList.toArray(new Predicate[predicatesList.size()]));
		}

		// 执行查询
		TypedQuery<Tuple> q = entityManager.createQuery(cq);

		List<Tuple> queryResult = q.getResultList();

		if (queryResult == null || queryResult.size() < 1) {
			return new ArrayList<>(0);
		}
		List<DefinedBundle> result = new ArrayList<>(queryResult.size());

		for (Tuple tuple : queryResult) {
			DefinedBundle bundle = new DefinedBundle();
			bundle.setBundleCode((String) tuple.get("bundleCode"));
			bundle.setType((String) tuple.get("type"));
			result.add(bundle);
		}
		return result;
	}
}
